import { login, logout, getInfo } from '@/http/modules/login'
import { getToken, setToken, removeToken } from '@/utils/token'
import { resetRouter } from '@/router'

const clientSetting = {
  grant_type: 'password',
  scope: '',
  username: '',
  password: '',
  client_id: 'Label_App', // RMS_App
  client_secret: '1q2w3e*'
}

const state = {
  token: getToken(),
  name: '',
  userName: '',
  surname: '',
  avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
  email: '',
  introduction: '',
  phoneNumber: '',
  roles: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, userName) => {
    state.userName = userName
  },
  SET_TEL: (state, phoneNumber) => {
    state.phoneNumber = phoneNumber
  },
  SET_AVATAR: (state, avatar) => {
    const url =
      'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
    state.avatar = avatar || url
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_SURNAME: (state, surname) => {
    state.surname = surname
  },
  CLEAN: state => {
    state.token = ''
    state.name = ''
    state.userName = ''
    state.avatar = ''
    state.email = ''
    state.introduction = ''
    state.phoneNumber = ''
    state.roles = []
    state.surname = ''
  }
}

const actions = {
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      clientSetting.username = username.trim()
      clientSetting.password = password
      login(clientSetting)
        .then(response => {
          // const { data } = response

          commit('SET_TOKEN', response.access_token)
          setToken(response.access_token)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout()
        .then(() => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('CLEAN')
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(response => {
          if (!response) {
            reject('验证失败, 请重新登录.')
          }
          const {
            userName,
            name,
            surname,
            phoneNumber,
            email,
            extraProperties
          } = response

          commit('SET_NAME', name)
          commit('SET_SURNAME', surname)
          commit('SET_USERNAME', userName)
          commit('SET_TEL', phoneNumber)
          commit('SET_AVATAR', extraProperties.Avatar)
          commit('SET_EMAIL', email)
          commit('SET_INTRODUCTION', extraProperties.Introduction)
          // await store.dispatch('app/applicationConfiguration')
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  },
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('CLEAN')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
